package com.zd.bms.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.zd.bms.bean.vo.ScenicSpotDetailVo;
import com.zd.bms.bean.vo.req.ScenicSpotListReqVo;
import com.zd.bms.service.IScenicSpotService;
import com.zd.common.bean.model.ScenicSpot;
import com.zd.common.bean.model.ScenicSpotExt;
import com.zd.common.common.utils.StringUtils;
import com.zd.common.dao.ScenicSpotDao;
import com.zd.common.dao.ScenicSpotExtDao;
import com.zd.common.dao.common.BaseDao;
import com.zd.common.dao.common.FindRequestEntity;
import com.zd.common.dao.common.OrderEnum;
import com.zd.common.dao.common.Page;
import com.zd.common.dao.common.WhereEnum;
import com.zd.common.service.impl.BaseServiceImpl;

@Service
public class ScenicSpotServiceImpl extends BaseServiceImpl<ScenicSpot> implements IScenicSpotService {
	@Autowired
	private ScenicSpotDao scenicSpotDao;

	@Autowired
	private ScenicSpotExtDao scenicSpotExtDao;

	public BaseDao<ScenicSpot> getBaseDao() {
		return scenicSpotDao;
	}

	public ScenicSpotDetailVo getScenicSpotDetail(Integer id) throws Exception {
		ScenicSpot scenicSpot = get(id);
		if (scenicSpot == null) {
			return null;
		}
		return getScenicSpotDetail(scenicSpot);
	}

	@Override
	public Page<ScenicSpotDetailVo> pageScenicSpotDetail(ScenicSpotListReqVo req) throws Exception {
		FindRequestEntity findReq = new FindRequestEntity();
		if (!StringUtils.isBank(req.getName())) {
			findReq.addWhere("name", WhereEnum.LIKE, "%" + req.getName() + "%");
		}
		findReq.addOrder("create_time", OrderEnum.DESC);

		Page<ScenicSpot> scenicSpotPage = page(findReq);
		Page<ScenicSpotDetailVo> resultPage = new Page<>();
		resultPage.setPage(scenicSpotPage.getPage());
		resultPage.setPageSize(scenicSpotPage.getPageSize());
		resultPage.setTotalRow(scenicSpotPage.getTotalRow());
		List<ScenicSpot> list = scenicSpotPage.getList();
		List<ScenicSpotDetailVo> resultVoList = new ArrayList<>();
		for (ScenicSpot scenicSpot : list) {
			resultVoList.add(getScenicSpotDetail(scenicSpot));
		}
		resultPage.setList(resultVoList);
		return resultPage;
	}

	private ScenicSpotDetailVo getScenicSpotDetail(ScenicSpot scenicSpot) throws Exception {
		ScenicSpotDetailVo resultVo = new ScenicSpotDetailVo();
		BeanUtils.copyProperties(scenicSpot, resultVo);
		FindRequestEntity findReq = new FindRequestEntity();
		findReq.addWhere("scenic_spot_id", WhereEnum.EQ, scenicSpot.getId());
		List<ScenicSpotExt> scenicSpotExtList = scenicSpotExtDao.find(findReq);
		if (scenicSpotExtList != null && scenicSpotExtList.size() > 0) {
			ScenicSpotExt scenicSpotExt = scenicSpotExtList.get(0);
			BeanUtils.copyProperties(scenicSpotExt, resultVo);
			resultVo.setId(scenicSpot.getId());
			resultVo.setScenicSpotExtId(scenicSpotExt.getId());
		}
		return resultVo;
	}

	@Override
	public Map<String, Object> saveOrUpdateScenicSpotDetail(ScenicSpotDetailVo detail) throws Exception {
		Map<String,Object> resultMap = new HashMap<>();
		if(detail.getId() == null || detail.getId() == 0) {
			//新增
			ScenicSpot scenicSpot = new ScenicSpot();
			scenicSpot.setName(detail.getName());
			scenicSpot.setArea(detail.getArea());
			scenicSpot.setAppId(detail.getAppId());
			scenicSpot.setAccountId(detail.getAccountId());
			insert(scenicSpot);
			
			ScenicSpotExt ext = new ScenicSpotExt();
			ext.setScenicSpotId(scenicSpot.getId());
			ext.setPayAccountId(detail.getPayAccountId());
			ext.setPayAppId(detail.getPayAppId());
			ext.setPaySecretKey(detail.getPaySecretKey());
			ext.setPayMchId(detail.getPayMchId());
			ext.setPayCertPath(detail.getPayCertPath());
			scenicSpotExtDao.insert(ext);
		} else  {
			//修改
			ScenicSpot scenicSpot = get(detail.getId());
			scenicSpot.setName(detail.getName());
			scenicSpot.setArea(detail.getArea());
			scenicSpot.setAppId(detail.getAppId());
			scenicSpot.setAccountId(detail.getAccountId());
			update(scenicSpot);
			
			ScenicSpotExt ext = scenicSpotExtDao.get(getScenicSpotDetail(scenicSpot).getScenicSpotExtId());
			ext.setScenicSpotId(scenicSpot.getId());
			ext.setPayAccountId(detail.getPayAccountId());
			ext.setPayAppId(detail.getPayAppId());
			ext.setPaySecretKey(detail.getPaySecretKey());
			ext.setPayMchId(detail.getPayMchId());
			ext.setPayCertPath(detail.getPayCertPath());
			scenicSpotExtDao.update(ext);
		}
		resultMap.put("result", true);
		return resultMap;
	}

	@Override
	public List<Map<String, Object>> getScenicSpotKeyValueList() throws Exception {
		FindRequestEntity findReq = new FindRequestEntity();
		findReq.addOrder("create_time", OrderEnum.DESC);
		List<ScenicSpot> scenicSpotList = find(findReq);
		List<Map<String, Object>> resultList = new ArrayList<>();
		for(ScenicSpot scenicSpot : scenicSpotList) {
			Map<String, Object> map = new  HashMap<>();
			map.put("key", scenicSpot.getId());
			map.put("value", scenicSpot.getName());
			resultList.add(map);
		}
		return resultList;
	}
}
